using Microsoft.NetEnterpriseServers;
using Microsoft.SqlServer.Management.UI;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace Microsoft.SqlServer.Management.SqlManagerUI
{
	internal class SelectDatabaseWizardPage : WizardPage
	{
		private Label idLabelDescription;

		private CheckBox chkScriptAllObjects;

		private ListBox idDatabasesList;

		private GenScriptWizForm parent;

		private GenWizGeneralData generalData;

		public SelectDatabaseWizardPage(WizardForm wizard) : base(wizard)
		{
			this.InitializeComponent();
			this.LoadStrings();
			this.parent = (GenScriptWizForm)wizard;
			base.PageID = 1;
			this.SubTitle = GenScriptWizSR.GenScriptSelectDatabaseSubtitle;
			this.Title = GenScriptWizSR.GenScriptSelectDatabaseTitle;
			base.HelpTopicKeyword = "sql9.swb.generatescriptswizard.selectdatabase.f1";
			this.idDatabasesList.AccessibleName = GenScriptWizSR.GenScriptSelectDatabaseTitle;
			this.generalData = (GenWizGeneralData)base.WizardInputs.FindItem(0);
		}

		protected override void OnInitializePage(EventArgs e)
		{
			this.Wizard.BackEnabled = true;
			base.WizardInputs.AddEditItem(3, true);
		}

		private void FillDBList()
		{
			bool flag = false;
			DataTable databases = this.generalData.GetDatabases();
			this.idDatabasesList.Items.Clear();
			if (databases != null)
			{
				for (int i = 0; i < databases.Rows.Count; i++)
				{
					string text = databases.Rows[i]["Name"].ToString();
					this.idDatabasesList.Items.Add(text);
					if (this.generalData.DatabaseName != null && this.generalData.DatabaseName.Length > 0 && this.generalData.DatabaseName == text)
					{
						flag = true;
						this.idDatabasesList.SelectedIndex = i;
					}
				}
				if (databases.Rows.Count > 0 && !flag)
				{
					this.idDatabasesList.SelectedIndex = 0;
				}
				if (!flag && !string.IsNullOrEmpty(this.generalData.DatabaseName))
				{
					this.idDatabasesList.Items.Add(this.generalData.DatabaseName.ToUpper());
					this.idDatabasesList.SelectedIndex = this.idDatabasesList.Items.Count - 1;
				}
			}
		}

		protected override void OnEnterPage(EventArgs e)
		{
			bool @checked = base.WizardInputs.FindBool(3);
			this.chkScriptAllObjects.Checked = @checked;
			this.EnableFinish();
			this.Wizard.NextEnabled = true;
			this.FillDBList();
		}

		protected override void OnLeavePage(LeavePageEventArgs e)
		{
			bool flag = false;
			bool flag2 = false;
			bool flag3 = false;
			bool flag4 = false;
			this.generalData.ScriptingDriver.Urns.Clear();
			if (this.idDatabasesList.SelectedItem != null)
			{
				if (this.generalData.DatabaseName != this.idDatabasesList.SelectedItem.ToString() || this.generalData.ConnectionStringDB)
				{
					flag = true;
				}
				if (base.WizardInputs.FindBool(3) != this.chkScriptAllObjects.Checked)
				{
					flag4 = true;
				}
				this.generalData.DatabaseName = this.idDatabasesList.SelectedItem.ToString();
				base.WizardInputs.AddEditItem(3, this.chkScriptAllObjects.Checked);
			}
			List<GenWizObjectType> list = (List<GenWizObjectType>)base.WizardInputs.FindItem(2);
			foreach (GenWizObjectType current in list)
			{
				if (!flag2)
				{
					bool flag5;
					this.generalData.GetObjectsList(current.PageId, true, out flag5);
					if (flag5)
					{
						flag2 = true;
					}
				}
				if (flag || flag4)
				{
					if (!flag3 && flag2)
					{
						this.parent.RemovePage(2);
						if (!this.chkScriptAllObjects.Checked)
						{
							this.parent.AddPage(new PickObjectTypesWizardPage(this.parent), 2, 1);
						}
						flag3 = true;
					}
					this.parent.RemovePage((int)current.PageId);
				}
			}
			if (!flag2 && (e.Button == WizardButton.Next || e.Button == WizardButton.Finish))
			{
				e.Cancel = true;
				this.parent.MessageProvider.ShowMessage(GenScriptWizSR.GenerateScriptNoObjects(this.generalData.DatabaseName), GenScriptWizSR.WizardText(""), ExceptionMessageBoxButtons.OK, ExceptionMessageBoxSymbol.Information, this);
			}
			if (base.WizardInputs.FindItem(1) == null)
			{
				if (this.chkScriptAllObjects.Checked)
				{
					base.WizardInputs.AddEditItem(1, new GenWizTransferOptions(this.generalData.SqlServerVersion));
					return;
				}
				base.WizardInputs.AddEditItem(1, new GenWizOptions(this.generalData.SqlServerVersion));
			}
		}

		private void LoadStrings()
		{
			this.idLabelDescription.Text = GenScriptWizSR.GenScriptSelectDatabaseGridLabel;
			this.chkScriptAllObjects.Text = GenScriptWizSR.GenScriptSelectDatabaseSelectAll;
		}

		private void InitializeComponent()
		{
			this.idLabelDescription = new Label();
			this.chkScriptAllObjects = new CheckBox();
			this.idDatabasesList = new ListBox();
			base.SuspendLayout();
			this.idLabelDescription.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right);
			this.idLabelDescription.ImeMode = ImeMode.NoControl;
			this.idLabelDescription.Location = new Point(16, 12);
			this.idLabelDescription.Name = "idLabelDescription";
			this.idLabelDescription.Size = new Size(432, 16);
			this.idLabelDescription.TabIndex = 0;
			this.idLabelDescription.Text = "A: &Select a database:";
			this.chkScriptAllObjects.Anchor = (AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.chkScriptAllObjects.CheckAlign = ContentAlignment.TopLeft;
			this.chkScriptAllObjects.Checked = true;
			this.chkScriptAllObjects.CheckState = CheckState.Checked;
			this.chkScriptAllObjects.Location = new Point(16, 219);
			this.chkScriptAllObjects.Name = "chkScriptAllObjects";
			this.chkScriptAllObjects.Size = new Size(432, 21);
			this.chkScriptAllObjects.TabIndex = 2;
			this.chkScriptAllObjects.Text = "A:Script &all objects in the selected database";
			this.chkScriptAllObjects.TextAlign = ContentAlignment.TopLeft;
			this.chkScriptAllObjects.CheckedChanged += new EventHandler(this.chkScriptAllObjects_CheckedChanged);
			this.idDatabasesList.Anchor = (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right);
			this.idDatabasesList.FormattingEnabled = true;
			this.idDatabasesList.Location = new Point(16, 36);
			this.idDatabasesList.Name = "idDatabasesList";
			this.idDatabasesList.Size = new Size(432, 173);
			this.idDatabasesList.TabIndex = 1;
			base.Controls.Add(this.idDatabasesList);
			base.Controls.Add(this.idLabelDescription);
			base.Controls.Add(this.chkScriptAllObjects);
			base.Name = "SelectDatabaseWizardPage";
			base.Size = new Size(464, 253);
			base.ResumeLayout(false);
		}

		private void chkScriptAllObjects_CheckedChanged(object sender, EventArgs e)
		{
			this.EnableFinish();
		}

		private void EnableFinish()
		{
			if (this.chkScriptAllObjects.Checked)
			{
				base.FinishEnabled = true;
				return;
			}
			base.FinishEnabled = this.parent.CanEnableFinish();
		}
	}
}
